var post_collection = db.collection("post");
(function () {
    // 构造函数
    var _con = function (info, author) {
        var self = this;
        if (info) {
            printData("正在创建话题：", info, author);
            self.type = "post";
            // 自有id
            self.id = "po_" + Math.random().toString(16).substr(2, 2) + (new Date()).getTime().toString(16);
            // 话题标题
            self.title = info.title;
            // 创建者
            self.creater = author || {};
            self.creater_id = author._id;
            // 创建时间
            self.createtime = (new Date()).getTime();
            // 内容
            self.detail = info.detail;
            // 所属版块
            self._module = info._module;
            self._module_name = info._module_name;
            // 封面
            self.preview_img = info.preview_img;
            // 阅读数
            self.viewtotal = info.viewtotal || 0;
            // 回复数
            self.replytotal = info.replytotal || 0;
        };
    };
    // 导出类;
    module.exports = _con;

    // 创建话题
    _con.prototype.save = co.wrap(function* (params) {
        var self = this;
        // console.log("self", self)
        if (!self.title) {
            return printError("话题标题不可为空");
        };
        if (!self.detail) {
            return printError("内容必须填写");
        };
        var result = yield mongo.insertOne(post_collection, self);
        return result;
    });

    // 获取所有话题
    _con.prototype.getAll = co.wrap(function* () {
        return yield mongo.find(post_collection, { query: {} });
    });

    // 更新话题
    _con.prototype.update = co.wrap(function* (info) {
        printData("正在更新话题信息：", info);
        var _id = info._id;
        delete info._id;
        info.updatetime = (new Date()).getTime();
        var doc = yield mongo.findOneAndUpdate(post_collection, {
            query: {
                _id: new ObjectID(_id)
            },
            data: info
        });
        return doc.value;
    });

    // 根据分页信息获取对应话题列表
    _con.prototype.getListByPage = co.wrap(function* (info) {
        printData("根据分页获取话题列表：", info);
        var docs = yield mongo.find(post_collection, {
            skip: info.num * info.page,
            limit: info.num,
            sort: {
                createtime: -1
            }
        });
        return docs;
    });

    // 根据版块信息获取对应话题列表
    _con.prototype.getListByModule = co.wrap(function* (info) {
        printData("根据版块信息获取对应话题列表：", info);
        var query;
        query = info._module ? {
            _module: info._module
        } : {}
        var docs = yield mongo.find(post_collection, {
            skip: info.num * info.page,
            limit: info.num,
            sort: {
                createtime: -1
            },
            query: query
        });
        return docs;
    });

    // 删除指定话题
    _con.prototype.delete = co.wrap(function* (_id) {
        var doc = yield mongo.deleteOneByObjectId(post_collection, {
            _id: _id
        });
        return doc;
    });

    // 删除对应版块的话题
    _con.prototype.deleteByModule = co.wrap(function* (_module) {
        printData("正在删除对应版块的话题：", _module);
        var result = yield mongo.delete(post_collection, {
            query: {
                _module: _module
            }
        });
        print(result);
    });

    // 获取话题详情
    _con.prototype.detail = co.wrap(function* (_id) {
        printData("获取话题详情：", _id);
        var doc = yield mongo.findOneByObjectId(post_collection, _id);
        return doc;
    });

    // 获取话题总数
    _con.prototype.count = co.wrap(function* (info) {
        return yield mongo.count(post_collection, info);
    });
} ());